<?php 
namespace XFCore\traits\admin;
use lib\Examine;
/*
*用户配置
*/
trait UserTrait{
    public function userstatus($get,$post){
        if($this->islogin_admin!=1)exitjson(-1,'请先登录');
        $pid = $post['pid'];
        if(empty($pid))exitjson(-1,'非法操作！');
        $userrow = $this->db->userrow($pid,'pid','zt');
        $params = [
            'zt' =>$userrow['zt'] == 1 ? 0 : 1,
        ];
        if($this->db->update('user',$params,['pid'=>$pid]))exitjson(200,'更新状态成功');
        exitjson(-1,'更新状态失败！');
    }
    public function Add_user($get , $post , $data){
        if($this->islogin_admin!=1)exitjson(-1,'请先登录');
        $qq      = $data['qq'];
        $user    = $data['user'];
        $email   = $data['email'];
        $pass    = $data['pass'];
        Examine::checkRequiredParams(array($qq , $email , $user , $pass));
        if($this->db->userrow($user,'user'))exitjson(-1,'该用户名已被平台用户注册过商户 请更换');
        if($this->db->userrow($qq,'qq'))exitjson(-1,'该QQ已在平台注册过账号 请更换');
        if($this->db->userrow($email,'email'))exitjson(-1,'该邮箱已经注册过商户 请更换');
        if (!empty($email) && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
            exitjson(500, "邮箱格式不正确");
        }
        if (!empty($user) && preg_match("/[\x{4e00}-\x{9fa5}]+/u", $user)) {
            exitjson(500, "用户名不能包含中文字符");
        }
        if(!empty($pass)){
            if(strlen($pass) < 6)exitjson(500, "密码长度不能少于6位");
            if(ctype_digit($pass))exitjson(500, "密码过于简单，请使用包含字母、数字和特殊字符的更复杂密码");
        }
        $salt = $this->salt;
       
        $data['pass'] = md5($salt . md5($salt.$pass.$salt.md5($salt.$pass.$salt).$pass.$salt) . $salt);//用户密码规则
        $data['salt'] = $salt;
        $data['pay_pass'] = md5(md5($data['pay_pass']));
        $data['pid'] = '1' . mt_rand(100000000, 999999999);
        $data['key']= md5('1'.mt_rand(10000000,99999999).$qq);
        $data['addtime'] = $this->date;
        $data['money'] = $data['money']?:'0.00';
        $data['INPUTFREE'] = 3;
        $data['outtime'] = 180;
        $data['username'] = $this->conf->sitiname . '商户';
        $params = [];
        foreach ($data as $k => $v) {
            if ($v !== '') {
                $params[$k] = $v;
            }
        }
        if($this->db->insert('user',$params))exitjson(200,'添加成功！');
        exitjson(-1,'添加失败！');
    }
    public function Edit_gaimi($get , $post , $postdata){
        if($this->islogin_admin!=1)exitjson(-1,'请先登录');
        $pid      = $postdata['id'];
        $pass     = $postdata['pass'];
        $pay_pass = $postdata['pay_pass'];
        
        Examine::checkRequiredParams(array($pay_pass , $pass , $pid));
        $userrow  = $this->db->userrow($pid,'pid');
        if(empty($userrow))exitjson(-1,'用户不存在');
        if(!empty($pass)){
            if(strlen($pass) < 6)exitjson(500, "密码长度不能少于6位");
            if(ctype_digit($pass))exitjson(500, "密码过于简单，请使用包含字母、数字和特殊字符的更复杂密码");
        }
        $salt = $this->salt;
        $data = [
            'pass'=>md5($salt . md5($salt.$pass.$salt.md5($salt.$pass.$salt).$pass.$salt) . $salt),//用户密码规则
            'salt'=>$salt,
            'pay_pass'=>md5(md5($pay_pass))
        ];
        if($this->db->update("user",$data,['pid'=>$pid])){
            createLog('修改用户密码成功！pid：'.$pid,http_ip(),'admin');
            exitjson(200,'修改用户密码成功');
        }
        exitjson(-1,'修改失败！');
    }
    public function Edit_user($get,$post,$data){
        if($this->islogin_admin!=1)exitjson(-1,'请先登录');
        $id = $data['id'];
        if(empty($id))exitjson(-1,'非法操作！');
        if(empty($data['qq']))exitjson(-1,'QQ和邮箱不可为空！');
        if (!empty($data['email']) && !filter_var($data['email'], FILTER_VALIDATE_EMAIL)) {
            exitjson(500, "邮箱格式不正确");
        }
        $userrow = $this->db->userrow($id,'pid');
        if(!$userrow)exitjson(-1,'用户不存在');
        unset($data['id']);
        if(safeSqlQuery("select * from `pre_user` where `qq`=:qq and `pid`<>:pid limit 1",['qq'=>$data['qq'],'pid'=>$id])['result'][0])exitjson(-1,'QQ不可重复！');
        $data['alipay_free_vip_time'] = !empty($data['alipay_free_vip_time']) ? $data['alipay_free_vip_time'] .date(" H:i:s"): '';
        $data['qqpay_free_vip_time']  = !empty($data['qqpay_free_vip_time']) ? $data['qqpay_free_vip_time'] .date(" H:i:s"): '';
        $data['wxpay_free_vip_time']  = !empty($data['wxpay_free_vip_time']) ? $data['wxpay_free_vip_time'] .date(" H:i:s") : '';
        $params = [];
        foreach ($data as $k=>$v){
            if($v !== ''){
                $params[$k] = $v;
            }
        }
        
        if($this->db->update("user",$params,['pid'=>$id])){
            createLog('修改用户成功！pid：'.$id,http_ip(),'admin');
            exitjson(200,'修改成功！');
        }
        exitjson(-1,'修改失败！');
    }
    public function userremove(){
        if($this->islogin_admin!=1)exitjson(-1,'请先登录');
        $pid = input("post")['id'];
        if(empty($pid))exitjson(-1,'非法操作！');
        $userrow = $this->db->userrow($pid,'pid');
        if(empty($userrow))exitjson(-1,'用户不存在');
        if($this->db->delete("user",['pid'=>$pid])){
            createLog('删除用户！pid：'.$pid,http_ip(),'admin');
            exitjson(200,'删除成功！');
        }
        exitjson(-1,'删除失败！');
    }
}
?>